package org.red5.core;

/*
 * RED5 Open Source Flash Server - http://www.osflash.org/red5
 * 
 * Copyright (c) 2006-2008 by respective authors (see below). All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation; either version 2.1 of the License, or (at your option) any later 
 * version. 
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this library; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

import org.red5.logging.Red5LoggerFactory;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.slf4j.Logger;

import com.meevle.business.UserDelegate;
import com.meevle.utilities.HibernateManager;

/**
 * Sample application that uses the client manager.
 * 
 * @author The Red5 Project (red5@osflash.org)
 */
public class Application extends ApplicationAdapter {

	private static Logger log = Red5LoggerFactory.getLogger(Application.class,
			"Meevle");
//	private static Object identity;

//	public void init()
//	{
//		identity = null;
//		try {
//			identity = HibernateManager.getInstance().createSession();
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//	}
	/** {@inheritDoc}
	 * @param params contains a UserVO object and a String object (the password)
	 * @throws IllegalArgumentException if the user, trying to connect, doesn't exist in the Database
	 */
	@Override
	public boolean connect(IConnection conn, IScope scope, Object[] params) throws IllegalArgumentException
	{
		boolean ret = false;

		String username = (String) params[0];
		String password = (String) params[1];

		try {
			ret = UserDelegate.login(username, password);
		} catch (IllegalArgumentException e) {
			throw e;
		}

		System.out.println("\n Connectie?: " + ret);
		
		return ret;
	}

	/** {@inheritDoc} */
	@Override
	public void disconnect(IConnection conn, IScope scope) {
		super.disconnect(conn, scope);
	}
	
//	public void destroy()
//	{
//		try {
//			HibernateManager.getInstance().closeSession(identity);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		super.appStop(scope);
//	}
	
	//TODO Multithreading
}
